package com.exam.action;


import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;

import com.exam.dao.GradeDao;
import com.exam.model.TestPaperForm;
import com.exam.service.GradeService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class TestFinishedAction implements ServletResponseAware,
ModelDriven<TestPaperForm> ,Preparable{
	PrintWriter out = null;
	private TestPaperForm paperForm;
	
	/** 解析答案的算法
	 * 提交试卷
	 * @return
	 */
	public String submitPaper(){
		int gid;
		int score = -1;
		try {
			Thread.sleep(1000);
			out = response.getWriter();
			/**
			 * 1.获取试卷数据
			 * 2.计算成绩 保存到数据库
			 * 3.返回用户的成绩id 查询到成绩 然后返回成绩
			 */
			System.out.println(paperForm.toString());
			
		    gid = new GradeService().saveGrade(paperForm);
		    
		    score = new GradeDao().getScore(gid);
		    
			out.print(score);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void prepareSubmitPaper(){
		paperForm = new TestPaperForm();
	}
	
	HttpServletResponse response;
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	@Override
	public TestPaperForm getModel() {
		return paperForm;
	}
	public TestPaperForm getPaperForm() {
		return paperForm;
	}
	public void setPaperForm(TestPaperForm paperForm) {
		this.paperForm = paperForm;
	}
	@Override
	public void prepare() throws Exception {
		//不执行
	}

}
